package com.poet.df.xml.elementhandler.support;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.dom4j.Attribute;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

import com.poet.df.xml.pojo.HtmlForm;
import com.poet.df.xml.pojo.HtmlFormItem;

@Component
public class AddItemElementHandler extends AbstractXmlElementHandler {

	public static final String ELEMENT = "add-item";

	@Override
	public void handlerElement(Element element, HtmlForm form) {
		List<Attribute> attributes = element.attributes();
		HtmlFormItem item = new HtmlFormItem();
		for( Attribute attr : attributes ) {
			String attrName = attr.getName();
			String attrValue = attr.getValue();
			try {
				BeanUtils.setProperty(item, attrName, attrValue);
			} catch (IllegalAccessException | InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		form.getItems().add(item);
		
	}

	@Override
	public String getElementName() {
		return ELEMENT;
	}
	
	
}
